package com.atguigu.gulimall.product.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.atguigu.common.valid.AddGroup;
import com.atguigu.common.valid.UpdateGroup;
import com.atguigu.common.valid.UpdateStatusGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.service.BrandService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

import javax.validation.Valid;


/**
 * 品牌
 *
 * @author chenjk
 * @email 1335239835@qq.com
 * @date 2024-08-20 21:26:32
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    //@RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    @GetMapping("/infos")
    public R info(@RequestParam("brandIs") List<Long> brandIds){
        List<BrandEntity> brand = brandService.getBrandsByIds(brandIds);
        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:brand:save")
    //只有使用了实体类属性用了 javax. validation对应的注解，
    //以及这边的函数使用@Valid 标注 才知道参数需要进行校验
    //brand后面可以接上 ,BindingResult result
    //调用result的hasErrors()，能够用来判断是否发生错误
    /*public R save(@Valid @RequestBody BrandEntity brand, BindingResult result){
		if(result.hasErrors()){
            Map<String,String> map=new HashMap<>();
            //获取校验错误结果
            result.getFieldErrors().forEach(item->{
                String defaultMessage = item.getDefaultMessage();
                String field = item.getField();
                map.put(field,defaultMessage);
            });

            return R.error(400,"不合法").put("data",map);
        }else{
            brandService.save(brand);
        }

        return R.ok();
    }*/
    /*public R save(@Valid @RequestBody BrandEntity brand*//*, BindingResult result*//*){
        *//*if(result.hasErrors()){
            Map<String,String> map=new HashMap<>();
            //获取校验错误结果
            result.getFieldErrors().forEach(item->{
                String defaultMessage = item.getDefaultMessage();
                String field = item.getField();
                map.put(field,defaultMessage);
            });

            return R.error(400,"不合法").put("data",map);
        }else{
            brandService.save(brand);
        }*//*
        //这里之所以这样写 是因为引入了@ControllerAdvice(basePackages = "com.atguigu.gulimall.product.controller") 由它处理
        brandService.save(brand);

        return R.ok();
    }*/
    public R save(@Validated({AddGroup.class}) @RequestBody BrandEntity brand/*, BindingResult result*/){
        /*if(result.hasErrors()){
            Map<String,String> map=new HashMap<>();
            //获取校验错误结果
            result.getFieldErrors().forEach(item->{
                String defaultMessage = item.getDefaultMessage();
                String field = item.getField();
                map.put(field,defaultMessage);
            });

            return R.error(400,"不合法").put("data",map);
        }else{
            brandService.save(brand);
        }*/
        //这里之所以这样写 是因为引入了@ControllerAdvice(basePackages = "com.atguigu.gulimall.product.controller") 由它处理
        brandService.save(brand);

        return R.ok();
    }


    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:brand:update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand){
		//现在不能只改一张品牌表了 因为有些关联表
        //brandService.updateById(brand);
        brandService.updateDetail(brand);
        return R.ok();
    }

    @RequestMapping("/update/status")
    //@RequiresPermissions("product:brand:update")
    public R updateStatus(@Validated({UpdateStatusGroup.class}) @RequestBody BrandEntity brand){
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
